package com.mediatek.factorymode;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Process;
import android.preference.PreferenceManager;
import android.provider.Settings; //Added by qxbin@20130819
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;
import android.util.Log;
//import com.mediatek.common.featureoption.FeatureOption;

//import dalvik.annotation.Signature;
import java.util.ArrayList;
import java.util.List;

public class FactoryMode extends Activity implements
		AdapterView.OnItemClickListener {
	public View.OnClickListener cl;
	private MyAdapter mAdapter;
	private Button mBtAll;
	private Button mBtAuto;
	private GridView mGrid;
	private String TAG = "FactoryMode";
	private int itemSize;
	private ArrayList<String> localArrayList;
	// @Signature({"Ljava/util/List", "<", "Ljava/lang/String;", ">;"})
	private List mListData;
	private SharedPreferences mSp = null;

	private void SetColor(TextView paramTextView) {
		SharedPreferences localSharedPreferences1 = getSharedPreferences(
				"FactoryMode", 0);
		this.mSp = localSharedPreferences1;
		for(int i=0; i < itemSize; i++){
			Resources localResources = getResources();
			String str1 = localArrayList.get(i);
			String str2 = paramTextView.getText().toString();
			String str4;
			if (str1.equals(str2)) {
				SharedPreferences localSharedPreferences2 = this.mSp;
				//int k = this.itemString[localObject];
				String str3 = localArrayList.get(i);//getString(k);
				str4 = localSharedPreferences2.getString(str3, null);
				if(str3.equals(getString(R.string.version))){
					str4 = Utils.getInt(this, "Version") == 1?"success":"default";
				}
				if (str4.equals("success")) {
					int l = getApplicationContext().getResources().getColor(
							R.color.Blue);
					// paramTextView.setTextColor(l);
					paramTextView
							.setBackgroundResource(R.drawable.mid_btn_success);
				} else if (str4.equals("default")) {
					int i1 = getApplicationContext().getResources().getColor(
							R.color.black);
					// paramTextView.setTextColor(i1);
					paramTextView
							.setBackgroundResource(R.drawable.mid_btn_default);

				} else if (str4.equals("failed")) {
					int i2 = getApplicationContext().getResources().getColor(
							R.color.Red);
					// paramTextView.setTextColor(i2);
					paramTextView
							.setBackgroundResource(R.drawable.mid_btn_fail);

				}

			}
		}
	}

	// @Signature({"()", "Ljava/util/List", "<", "Ljava/lang/String;", ">;"})
	private List getData() {
		boolean bool1 = true;
		ArrayList getList = new ArrayList();
		SharedPreferences localSharedPreferences = PreferenceManager
				.getDefaultSharedPreferences(this);
		for(int i=0; i < itemSize; i++){
			String str1 = localArrayList.get(i);
			boolean bool2 = localSharedPreferences.getBoolean(str1, bool1);
			if (bool1 == bool2) {
				String str2 = localArrayList.get(i);
				getList.add(str2);
			}
		}
		return getList;
	}

	private void init() {
		String str1 = "default";
		SharedPreferences localSharedPreferences = getSharedPreferences(
				"FactoryMode", 0);
		this.mSp = localSharedPreferences;
		SharedPreferences.Editor localEditor = this.mSp.edit();
		for(int i=0; i < itemSize; i++){
			String str2 = localArrayList.get(i);
			String exist = localSharedPreferences.getString(str2, null);
			if (exist == null)
				localEditor.putString(str2, str1);
		}
		String str3 = getString(R.string.headsethook_name);
		localEditor.putString(str3, str1);
		localEditor.commit();
	}

	public void onCreate(Bundle paramBundle) {
		requestWindowFeature(1);
		super.onCreate(paramBundle);
		setContentView(R.layout.main);

		localArrayList = new Utils().getListArray(getResources());
		itemSize = localArrayList.size();

		init();
		Log.e(TAG, "onCreate");

		this.mGrid = (GridView) findViewById(R.id.main_grid);
		this.mListData = getData();
		this.mAdapter = new MyAdapter(this);
	}

	protected void onDestroy() {
		super.onDestroy();
		Log.e(TAG, "onDestroy");

		//Process.killProcess(Process.myPid());
	}

	// @Signature({"(", "Landroid/widget/AdapterView", "<*>;",
	// "Landroid/view/View;", "IJ)V"})
	public void onItemClick(AdapterView paramAdapterView, View paramView,
			int paramInt, long paramLong) {
		Intent localIntent = new Intent();
		localIntent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
		String str1 = (String) this.mListData.get(paramInt);
		String str2 = null;
		Log.e(TAG, "onItemClick");
		str2 = new Utils().getTestPkgName(getResources(), str1);
		if (str2 != null) {
			Log.e(TAG, "str2");

			localIntent.setClassName(this, str2);
			startActivity(localIntent);
		}
	}

	protected void onResume() {
		super.onResume();
		Log.e(TAG, "onResume");

		this.mGrid.setAdapter(this.mAdapter);
		this.mGrid.setOnItemClickListener(this);
	}

	public class MyAdapter extends BaseAdapter {
		private LayoutInflater mInflater;

		public MyAdapter(Context arg2) {
			// Context localContext;
			LayoutInflater localLayoutInflater = LayoutInflater.from(arg2);
			this.mInflater = localLayoutInflater;
		}

		public MyAdapter(FactoryMode paramInt, int arg3) {
		}

		public int getCount() {
			if (FactoryMode.this.mListData == null)
				return 0;
			return FactoryMode.this.mListData.size();
		}

		public Object getItem(int paramInt) {
			return Integer.valueOf(paramInt);
		}

		public long getItemId(int paramInt) {
			return paramInt;
		}

		public View getView(int paramInt, View paramView,
				ViewGroup paramViewGroup) {
			View localView = this.mInflater.inflate(R.layout.main_grid, paramViewGroup, false);
			TextView localTextView = (TextView) localView
					.findViewById(R.id.factor_button);
			CharSequence localCharSequence = (CharSequence) FactoryMode.this.mListData
					.get(paramInt);
			localTextView.setText(localCharSequence);
			FactoryMode.this.SetColor(localTextView);
			return localView;
		}
	}
}
